package com.pinc.akb48;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdView;

public class WebviewActivity extends Activity implements AdListener {
	protected WebView wv;
	protected TextView tv_title;
	private ImageView iv_next;
	private ImageView iv_back;
	private ImageView iv_reload;
	protected ProgressBar pgb;
	protected LinearLayout ll_progress;
	protected AdView adview;

	public class myChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress >= 100)
				ll_progress.setVisibility(View.INVISIBLE);
			else
				ll_progress.setVisibility(View.VISIBLE);
			pgb.setProgress(newProgress);
		}
	}

	protected void initWidget() {
		wv = (WebView) this.findViewById(R.id.webView1);
		tv_title = (TextView) this.findViewById(R.id.textView_title);
		ll_progress = (LinearLayout) this
		.findViewById(R.id.linearLayout_Progress);
		pgb = (ProgressBar) this.findViewById(R.id.progressBar1);

		iv_next = (ImageView) this.findViewById(R.id.imageButton1);
		iv_back = (ImageView) this.findViewById(R.id.imageButton2);
		iv_reload = (ImageView) this.findViewById(R.id.imageButton3);

		wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		wv.getSettings().setJavaScriptEnabled(true);
		wv.getSettings().setBuiltInZoomControls(true);
		wv.getSettings().setPluginsEnabled(true);
		wv.setWebChromeClient(new myChromeClient());
		wv.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.startsWith("vnd.youtube:")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(url));
					WebviewActivity.this.startActivity(intent);
					return (true);
				}
				view.loadUrl(url);
				return false;
			}
		});

		iv_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv.goBack();
				loadAD();
			}
		});

		iv_next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv.goForward();
				loadAD();
			}
		});

		iv_reload.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				wv.reload();
				loadAD();
			}
		});

		adview = (AdView) this.findViewById(R.id.adView);
		adview.setAdListener(this);
	}

	@Override
	public void onDismissScreen(Ad arg0) {
	}

	@Override
	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
	}

	@Override
	public void onLeaveApplication(Ad arg0) {
	}

	@Override
	public void onPresentScreen(Ad arg0) {
	}

	@Override
	public void onReceiveAd(Ad arg0) {
		adview.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onResume() {
		super.onResume();
		loadAD();
	}

	protected void loadAD() {
		adview.setVisibility(View.GONE);
		loadADwoGone();
	}

	protected void loadADwoGone() {
		AdRequest request = new AdRequest();
		request.setGender(AdRequest.Gender.MALE);
		adview.loadAd(request);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ( keyCode == KeyEvent.KEYCODE_BACK)
		{
			showDialog(ID_EXIT);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private static final int ID_EXIT = 2;

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch (id) {
		case ID_EXIT: {
			builder.setMessage(
					getResources().getString(R.string.str_exit_notify))
					.setPositiveButton(
							getResources().getString(R.string.str_yes),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									WebviewActivity.this.finish();
								}
							})
							.setNegativeButton(
									getResources().getString(R.string.str_no), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}
}
